package com.situ.day5;

import org.junit.Test;

import java.util.Arrays;

public class StringDemo {
    @Test
    public void test1() {
        // "abc"字符串常量，也是一个对象
        String str1 = "abc";
        String str2 = new String("abc");
        String str3 = "abc";
        //==比较的是地址值
        System.out.println(str1 == str2);// false
        System.out.println(str1 == str3);// true
        System.out.println(str2 == str3);// false
        //equals()比较字符串的内容是不是相等
        System.out.println(str1.equals(str2));//true
        System.out.println(str1.equals(str3));//true
        System.out.println(str2.equals(str3));//true
    }

    @Test
    public void test2() {
        String str = "java AndroidA";
        System.out.println(str.length());//13
        char[] chars = str.toCharArray();
        char ch = str.charAt(5);//A
        System.out.println(ch);
        System.out.println(str.indexOf('A'));//5
        System.out.println(str.indexOf('X'));//-1
        System.out.println(str.indexOf('A', 6));//12
        System.out.println(str.indexOf("And"));//5
        System.out.println(str.lastIndexOf("A"));//12
    }

    @Test
    public void test3() {
        String str1 = "java";
        String str2 = "Java";
        System.out.println(str1.equals(str2));//false
        System.out.println(str1.equalsIgnoreCase(str2));//true

        String str = "java AndroidA";
        System.out.println(str.contains("And"));//true
        System.out.println(str.startsWith("java"));//true
        System.out.println(str.startsWith("java A"));//true
        //offset: 偏移
        System.out.println(str.startsWith("And", 5));//true

        System.out.println(str.endsWith("idA"));//true
        //isHuiWen = true
        System.out.println(str.isEmpty());//false
        // "" 空字符串，也是一个对象
        System.out.println("".isEmpty());//true
        char ch = ' ';
    }

    @Test
    public void test44() {
        // ""
        //String str = "java And";
        char[] array = {'j', 'a', 'v', 'a', ' ', 'A', 'n', 'd'};
        String str = new String(array);
        char[] chars = str.toCharArray();
        System.out.println(chars);
        System.out.println(Arrays.toString(chars));
    }

    @Test
    public void test424() {
        String str = "java AndroidA";
        String newStr = str.replace('A', 'B');
        System.out.println(str);//java AndroidA
        System.out.println(newStr);//java BndroidB
    }

    @Test
    public void test45() {
        String str = "java android python";
        String[] array = str.split(" ");
        System.out.println(Arrays.toString(array));
    }

    @Test
    public void test87() {
        // subtree: 子树
        // substring:子字符串
        String str = "java AndroidA";
        System.out.println(str.substring(5));//AndroidA
        System.out.println(str.substring(5, 8));//And
    }

    @Test
    public void test181() {
        String str = "    java AndroidA    ";
        String newStr = str.trim();
        System.out.println(str);//    java AndroidA
        System.out.println(newStr);//java AndroidA
    }

    @Test
    public void test56() {
        //编译器有优化
        String string = "Java" + "PHP" + "Android"  + "UI";
        String str1 = "Java";
        String str2 = "PHP";
        String str3 = "Python";
        String str4 = "UI";
        String str = str1 + str2 + str3 + str4;
        System.out.println(str);

        StringBuffer buffer = new StringBuffer();
        //buffer.append(str1);
        //buffer.append(str2);
        //buffer.append(str3);
        //buffer.append(str4);
        //链式编程
        buffer.append(str1).append(str2).append(str3).append(str4);
        System.out.println(buffer);

        String s = buffer.toString();
    }

    @Test
    public void test88() {
        System.out.println(Integer.MAX_VALUE);

        //自动装箱和自动拆箱
        Integer integer1 = Integer.valueOf(4);
        System.out.println(integer1);
        Integer integer2 = 4;//自动装箱
        System.out.println(integer2);
        int i = integer2.intValue();
        int num = integer2;//自动拆箱

        Integer integer = 3;
        int num2 = integer + 4;
        integer = integer + 5;

        String str = "Hello Java";
        System.out.println(str.toUpperCase());
        System.out.println(str.toLowerCase());
    }

}
